/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entidade;

import controlo.util.Conexao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.model.SelectItem;

/**
 *
 * @author Lomanda
 */
public class TipoPermissaoUsuarioDAO {

    Connection conn;

    public TipoPermissaoUsuarioDAO() {
        try {
            conn = Conexao.getConnection();
        } catch (Exception ex) {
            Logger.getLogger(ContaDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void gravar(List<Integer> idPList, int idG) throws SQLException, Exception  {
        
            PreparedStatement ps;
            ps = conn.prepareStatement("DELETE FROM tipo_permissao_usuario WHERE idTipoUsuario = ?");
            ps.setInt(1, idG);
            ps.execute();

            for (Integer i : idPList) {
                ps = conn.prepareStatement("INSERT INTO tipo_permissao_usuario (idTipoUsuario, idPermissoes) VALUES (?, ?)");
                ps.setInt(1, idG);
                ps.setInt(2, i);
                ps.execute();
            }
            Conexao.closeConnection(conn,ps);
    }

    public void apagarPermissoes(int idG) throws SQLException  {
            PreparedStatement ps;
            ps = conn.prepareStatement("DELETE FROM tipo_permissao_usuario WHERE idTipoUsuario = ?");
            ps.setInt(1, idG);
            ps.execute();
    }

    
}
